package xin.clips.quartz.task;

import cn.hutool.json.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import xin.clips.common.utils.DateUtils;
import xin.clips.common.utils.StringUtils;
import xin.clips.system.domain.gy.GyArmParam;
import xin.clips.system.domain.gy.GyDeviceSource;
import xin.clips.system.domain.gy.GyMachineParam;
import xin.clips.system.mapper.gy.GyArmParamMapper;
import xin.clips.system.mapper.gy.GyDeviceMapper;
import xin.clips.system.mapper.gy.GyDeviceSourceMapper;
import xin.clips.system.mapper.gy.GyMachineParamMapper;

import java.util.Date;
import java.util.List;

/**
 * 定时同步设备数据
 * 
 * @author zhushanglin
 */
@Component("synDeviceTask")
public class SynDeviceTask {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    public GyDeviceMapper deviceMapper;

    @Autowired
    private GyMachineParamMapper machineParamMapper;

    @Autowired
    private GyArmParamMapper armParamMapper;

    @Autowired
    private GyDeviceSourceMapper deviceSourceMapper;

    public void perMin() {
        List<GyDeviceSource> allDeviceSources = deviceSourceMapper.getAllDeviceSources();
        JSONArray jsonArray = new JSONArray(allDeviceSources);
        logger.info(jsonArray.toString());

        allDeviceSources.forEach(e -> {
            String name = e.getName();
            String value = e.getValue().toString();
            Date time = e.getTime();
            if(StringUtils.isNotEmpty(name)){
                String[] split = name.split("-");
                String deviceSource = split[0]; //arm001
                String deviceField = split[1];
                Integer deviceId = deviceMapper.getDeviceIdByDeviceSource(deviceSource);
                if(StringUtils.isNotEmpty(deviceSource) && deviceSource.contains("arm")){
                    GyArmParam armParam = armParamMapper.getArmParamByDeviceId(deviceId);
                        switch (deviceField) {
                            case "speed":
                                logger.info("  =========  机械臂，字段: {}  =========  ",deviceField);
                                armParam.setRev(value);
                                break;
                            case "one":
                                logger.info("  =========  机械臂，字段: {}  =========  ",deviceField);
                                armParam.setOne(value);
                                break;
                            case "two":
                                logger.info("  =========  机械臂，字段: {}  =========  ",deviceField);
                                armParam.setTwo(value);
                                break;
                            case "three":
                                logger.info("  =========  机械臂，字段: {}  =========  ",deviceField);
                                armParam.setThree(value);
                                break;
                            case "four":
                                logger.info("  =========  机械臂，字段: {}  =========  ",deviceField);
                                armParam.setFour(value);
                                break;
                            case "five":
                                logger.info("  =========  机械臂，字段: {}  =========  ",deviceField);
                                armParam.setFive(value);
                                break;
                            case "six":
                                logger.info("  =========  机械臂，字段: {}  =========  ",deviceField);
                                armParam.setSix(value);
                                break;
                            default:
                                logger.info("  =========  机械臂，未知字段: {}  =========  ",deviceField);
                                break;
                        }
                        armParam.setUpdateTime(DateUtils.getNowDate());
                        armParamMapper.updateGyArmParam(armParam);

                }else if(StringUtils.isNotEmpty(deviceSource) && deviceSource.contains("machine")){
                    GyMachineParam machineParam = machineParamMapper.getMachineParamByDeviceId(deviceId);
                        switch (deviceField) {
                            case "production":
                                logger.info("  =========  数控机床，字段: {}   =========   ",deviceField);
                                machineParam.setProduction(value);
                                break;
                            case "loop_meter":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setLoopMeter(value);
                                break;
                            case "x_moto_temperature":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setxMotoTemperature(value);
                                break;
                            case "x_moto_load":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setxMotoLoad(value);
                                break;
                            case "justfy_moto_temperature":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setJustfyMotoTemperature(value);
                                break;
                            case "justfy_moto_load":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setJustfyMotoLoad(value);
                                break;
                            case "process_time":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setProcessTime(value);
                                break;
                            case "cut_time":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setCutTime(value);
                                break;
                            case "start_time":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setStartTime(value);
                                break;
                            case "run_time":
                                logger.info("  =========  数控机床，字段: {}  =========  ",deviceField);
                                machineParam.setRunTime(value);
                                break;
                            default:
                                logger.info("  =========  数控机床，未知字段: {}  =========  ",deviceField);
                                break;
                        }
                        machineParam.setUpdateTime(DateUtils.getNowDate());
                        machineParamMapper.updateGyMachineParam(machineParam);


                }
            }

        });

    }


}
